<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="比赛名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="比赛名称"></el-input>
      </el-form-item>
      <el-form-item label="项目类型" prop="types">
        <div style="display: flex;flex-direction: column;">
          <span v-for="k, i in dataForm.types" :key="i">
            <el-tag style="font-size: 22px;" size="medium">{{ k }}</el-tag>
          </span>
        </div>
        <el-button type="primary" icon="el-icon-plus" circle @click="showAddType = true"></el-button>

      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <!-- <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input> -->
        <el-date-picker v-model="dataForm.startTime" value-format="timestamp" type="datetime" placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <!-- <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input> -->
        <el-date-picker v-model="dataForm.endTime" value-format="timestamp" type="datetime" placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否开启" prop="statu">
        <!-- <el-input v-model="dataForm.statu" placeholder="是否开启"></el-input> -->
        <el-radio-group v-model="dataForm.statu">
          <el-radio :label="0">关闭</el-radio>
          <el-radio :label="1">启用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

    <el-dialog :visible.sync="showAddType" width="30%" append-to-body>
      <div>{{ '添加项目类型' }}</div>
      <el-tag style="margin: 0 5px;" :key="k" v-for="k in dataForm.types" closable :disable-transitions="false"
        @close="handleClose(k)">
        {{ k }}
      </el-tag>

      <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" size="small"
        @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm" style="max-width: 120px;">
      </el-input>
      <el-button style="display: inline;" v-else class="button-new-tag" @click="showInput" type="info">添加类型</el-button>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="showAddType = false">取 消</el-button> -->
        <el-button type="primary" @click="addType()">确 定</el-button>
      </span>
    </el-dialog>

  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      showAddType: false,
      typeList: [],
      //添加类型窗口
      inputVisible: false,
      inputValue: '',
      dataForm: {
        id: 0,
        name: '',
        typeList: [],
        types: [],
        startTime: '',
        endTime: '',
        statu: ''
      },
      dataRule: {
        name: [{
          required: true,
          message: '比赛名称不能为空',
          trigger: 'blur'
        }],
        types: [{
          required: true,
          message: '项目类型不能为空',
          trigger: 'blur'
        }],
        startTime: [{
          required: true,
          message: '开始时间不能为空',
          trigger: 'blur'
        }],
        endTime: [{
          required: true,
          message: '结束时间不能为空',
          trigger: 'blur'
        }],
        statu: [{
          required: true,
          message: '是否开启不能为空',
          trigger: 'blur'
        }]
      }
    }
  },

  mounted() {
    this.getTypeList()
  },

  deactivated() {
    this.dataForm.types = []
  },
  methods: {
    showInput() {
      this.inputVisible = true
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dataForm.types.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    getTypeList() {
      this.$http({
        url: this.$http.adornUrl(`/sys/type/list`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        // this.typeList = data.page.list
      })

    },
    handleClose(k) {
      this.dataForm.types.splice(this.dataForm.types.indexOf(k), 1);
    },
    addType() {

      this.showAddType = false
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        // this.dataForm.types = []
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/project/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.project.name
              this.dataForm.startTime = data.project.stime
              this.dataForm.endTime = data.project.etime
              this.dataForm.statu = data.project.statu
              this.dataForm.types = data.project.types
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/project/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'startTime': this.dataForm.startTime,
              'endTime': this.dataForm.endTime,
              'statu': this.dataForm.statu,
              'types': this.dataForm.types,
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
